/**
 * Copyright (C) Octlan Corporation. All rights reserved.
 * 
 * @author todzhang
 * @date 2010-2-24
 * @version $Id: PeoPleListActivity.java 11 2010-03-03 06:44:06Z todzhang $
 */
package com.octlan.droid.mii.activity;

import android.app.Activity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.octlan.droid.mii.R;
import com.octlan.droid.mii.adapter.PeopleListCursorAdapter;

public class PeoPleListActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.peoplelist);

		final Cursor c = managedQuery(People.CONTENT_URI, null, null, null, null);
		String[] from = new String[] { People._ID, People.NAME, People.NUMBER };
		int[] to = new int[] { R.id.TextViewPeopleItemID, R.id.TextViewPeopleItemName,
				R.id.TextViewPeopleItemPrimaryPhone };

		CursorAdapter adapter = new PeopleListCursorAdapter(this, R.layout.peopleitem, c, from, to);
		ListView lv = (ListView) findViewById(R.id.ListViewPeopleList);
		lv.setAdapter(adapter);

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
				// Move the cursor to the selected item
				c.moveToPosition(pos);

				Intent i = new Intent(Intent.ACTION_CALL);
				long phoneID = c.getLong(c.getColumnIndex(People.PRIMARY_PHONE_ID));
				i.setData(ContentUris.withAppendedId(android.provider.Contacts.Phones.CONTENT_URI, phoneID));
				startActivity(i);
				// finish();
			}
		});
	}
}
